// document.write("<script language=javascript src='../carSeries.js'></script>");
// all表格
window.onload = function(){
    //初始化Table
    // 隐藏控件
    var pageAll= this.document.getElementById("pageAll")
    pageAll.style.display="block"
    var pageSearch= this.document.getElementById("pageSearch")
    pageSearch.style.display="none"
    
    // 封装到外面
    var url = 'http://192.168.0.110:8080/CarManage/getAllBrandInfo'
    function r(datas) {
        function operateFormatter(value, row, index) {//赋予的参数
            return [
                '<button class="btn btn-primary" style="margin-right:10px" onclick="editBrand(' + row.brandid + ')">编辑</button>',
                '<button class="btn btn-primary" onclick="seriesBrand(' + row.brandid + ')">系列</button>',
            ].join('');
        }
        // 设置table列样式
        function  formatFormatter(value, row, index) {
            if (value == 1){
            
            return [
               
                '<img src="../../../images/success.png" onclick="statusChange(' + row.validflag+','+row.brandid + ')">',
            ].join('');
        }else{
            return [
               
                '<img src="../../../images/fail.png" onclick="statusChange(' + row.validflag +','+row.brandid + ')">',
            ].join('');
        }
        }
        var tableColumns = [
            {
                field: 'brandid',
                title: '品牌ID',
                switchable: true
            }, {
                field: 'name',
                title: '名称',
                switchable: true
            }, {
                field: 'prefix',
                title: '拼音首字母',
                switchable: true
            }, {
                field: 'validflag',
                title: '状态',
                // formatter: formatCell,       //设置显示启用或禁用的文字
                formatter:formatFormatter    // 设置table操作按钮列样式
            }, {
                field: 'operate',
                title: '操作',
                formatter: operateFormatter //自定义方法，添加操作按钮
            }
        ];
        $("#table").bootstrapTable('destroy'); //分页必须写
        $("#table").bootstrapTable({
            cache: false,
            pagination: true,
            sidePagination: "client",  //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,   //初始化加载第一页，默认第一页
            pageSize: 10,   //每页的记录行数（*）
            columns: tableColumns,
            data: datas,
            queryParamsType: '',
            //得到查询的参数
            queryParams: function queryParams(params) {
                return params;
            },
            onLoadSuccess: function (data) {  //加载成功时执行
                alert("成功" + data)
            },
            onLoadError: function () {  //加载失败时执行
                alert("失败")
            }

        });
    }
    var currentPage = 1
    var pageSize = 10
    // 判断是否为当前页，或者最后一页，隐藏上一页或者下一页
    if (currentPage == 1 ) {
        $(".page_prev").css({ display: "none" });
    }else if( currentPage == totalPage){
        $(".page_next").hide();
    }
    $.ajax({
        url: url,//请求后台的url
        method: 'post',//请求方式
        contentType: "application/x-www-form-urlencoded",
        data: {
            currentPage: currentPage,
            pageSize: pageSize
        },
        success: function (res) {
            console.log(res.data.list)
            var total = res.data.total
            // 有余数向上取整
            var totalPage = Math.ceil(total / 10)
            r(res.data.list)
            //上一页
            $(".page_prev").click(function () {
                if (currentPage > 1) {
                    currentPage--;
                    $("#currentPage").text('当前第'+currentPage+'页');
                    console.log(currentPage)
                    $.ajax({
                        url: url,//请求后台的url
                        method: 'post',//请求方式
                        contentType: "application/x-www-form-urlencoded",
                        data: {
                            currentPage: currentPage,
                            pageSize: pageSize,
                        },
                        success: function (res) {
                            console.log(res)
                            console.log(res.data.total + '总数')
                            r(res.data.list)
                        },
                        error: function () {
                            console.log('错误')
                        }
                    })
                    $(".page_prev").css({ display: "inline-block" });
                }
               
                 if (currentPage == 1) {
                    $(".page_prev").css({ display: "none" });
                }
                if (currentPage < totalPage) {
                    $(".page_next").css({ display: "inline-block" });
                }
            });
            //显示总条数
             $("#total").text('共'+total+'条记录');
            //显示总页数
            $("#totalPage").text('共'+totalPage+'页');
             //当前页
             $("#currentPage").text('当前第'+currentPage+'页');
            //下一页
            $(".page_next").click(function () {
                if (currentPage < totalPage) {
                    currentPage++;
                    console.log(currentPage)
                    $("#currentPage").text('当前第'+currentPage+'页');
                    $.ajax({
                        url: url,//请求后台的url
                        method: 'post',//请求方式
                        contentType: "application/x-www-form-urlencoded",
                        data: {
                            currentPage: currentPage,
                            pageSize: pageSize,
                        },
                        success: function (res) {
                            console.log(res)
                            console.log(res.data.total + '总数')
                            r(res.data.list)
                        },
                        error: function () {
                            console.log('错误')
                        }
                    })
                    $(".page_next").css({ display: "inline-block" });

                }
                if (currentPage > 1) {
                    $(".page_prev").css({ display: "inline-block" });
                }
                if (currentPage == totalPage) {
                    $(".page_next").hide();
                }
            });
        },

    })
};